package com.junyidao.game.service;

import com.github.pagehelper.PageHelper;
import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.mapper.PCodeMapper;
import com.junyidao.game.mapper.PSequenceMapper;
import com.junyidao.game.pojo.PCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HP on 2016/8/7.
 */
@Service
@Transactional
public class CodeService {

    @Autowired
    private PCodeMapper pCodeMapper;

    @Autowired
    private PSequenceMapper sequenceMapper;

    public List<PCode> getAll(PCode pCode) {
        if (pCode.getPage() != null && pCode.getRows() != null) {
            PageHelper.startPage(pCode.getPage(), pCode.getRows(), "CODE_ID");
        }
        return pCodeMapper.selectAll();
    }

    public void add(PCode pCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("seqId", "CODE_ID");
        Integer id = sequenceMapper.getNextSequence(map);
        pCode.setCodeId(id);
        pCodeMapper.insert(pCode);
    }

    public List<PCode> select(String codeSortId) {
        PCode pCode = new PCode();
        pCode.setCodeSortId(codeSortId);
        pCode.setCodeStatus(BaseConstant.STATUS_NORMAL);
        return pCodeMapper.select(pCode);
    }
}
